package com.ai.task;

import com.ai.common.constant.enumP.CameraNetStatusEnum;
import com.ai.common.constant.enumP.CameraNetTimeEnum;
import com.ai.entity.CameraNetStatusRecord;
import com.ai.entity.made.camera.CameraPO;
import com.ai.service.made.CameraService;
import com.ai.service.made.ICameraNetStatusRecordService;
import com.ai.util.PingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 每三分钟检测一遍所有的摄像机通信状态
 * @Author jiaok
 * @Date 2023-10-30 16:54
 */
@Slf4j
//@Component
//@Configuration
//@EnableScheduling
public class CameraDetectionTask {

    @Resource
    CameraService iCameraService;

    @Resource
    ICameraNetStatusRecordService iCameraNetStatusRecordService;


    @Scheduled(cron = "0/5 * * * * ?")
    public void detectionTimer() {

        List<CameraPO> camerasList = iCameraService.list();
        //首先获取所有的摄像头的ip
//        List<> camerasList = iCamerasService.list();
        camerasList.forEach(item->{
            //查看当前摄像头的状态
            String cameraIp = item.getIp();
            boolean pingStatus = PingUtil.ping(cameraIp);//获取当前摄像头的通信状态
            String ping=pingStatus?CameraNetStatusEnum.NORMAL.getType() : CameraNetStatusEnum.UN_NORMAL.getType();
            //首先查看记录表表中是否有当前ip的摄像机记录-------------每次通信状态的变化都会进行记录，方便后期查看
            CameraNetStatusRecord netStatusRecordDB= iCameraNetStatusRecordService.getCameraNetRecord(item.getId(),item.getIp());
            if (null == netStatusRecordDB ){
                //说明是第一次检测当前的摄像头状态
                CameraNetStatusRecord netStatusRecord = getCameraNetStatusRecord(item, pingStatus);
                iCameraNetStatusRecordService.save(netStatusRecord);

            }else {
                //记录已经存在，说明已经检测过，比对检测结果
                if (!ping.equals(netStatusRecordDB.getCameraStatus())){
                    //两个结果不相同，添加新的摄像头状态记录，并将原本记录latest字段设为0
                    CameraNetStatusRecord netStatusRecord = getCameraNetStatusRecord(item, pingStatus);
                    iCameraNetStatusRecordService.save(netStatusRecord);
                    iCameraNetStatusRecordService.updateUNLatest(netStatusRecordDB.getId());
                }

                //相同则不作改变
            }
        });

    }

    /**
     * 新增摄像头状态变化记录
     * @param item 摄像头信息
     * @param pingStatus 通信状态
     * @return
     */
    private static CameraNetStatusRecord getCameraNetStatusRecord(CameraPO item, boolean pingStatus) {
        CameraNetStatusRecord netStatusRecord = new CameraNetStatusRecord();
        netStatusRecord.setIsLatest(CameraNetTimeEnum.LATEST.getType());
        netStatusRecord.setCameraStatus(pingStatus ?CameraNetStatusEnum.NORMAL.getType():CameraNetStatusEnum.UN_NORMAL.getType());
        netStatusRecord.setCameraId(item.getId());
        netStatusRecord.setCameraIp(item.getIp());
        netStatusRecord.setCreateTime(LocalDateTime.now());
        return netStatusRecord;
    }
}
